package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import server.connector.MyConnector;
import server.container.*;
import server.servlet.HttpServlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

// 主功能类
public class Minicat {

    MyServer myServer;

    public Minicat() throws ClassNotFoundException {
        InputStream inputStream = Minicat.class.getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootEl = document.getRootElement();

            Element serverEl = (Element) rootEl.selectNodes("//Server").get(0);
            myServer = new MyServer();

            Element serviceEl = (Element) rootEl.selectNodes("//Service").get(0);
            MyService myService = new MyService(myServer);

            Element connectorEl = (Element) rootEl.selectNodes("//Connector").get(0);
            MyConnector myConnector = new MyConnector(myService, connectorEl.attributeValue("port"));

            Element engineEl = (Element) rootEl.selectNodes("//Engine").get(0);
            MyEngine myEngine = new MyEngine(myService);

            Element hostEl = (Element) rootEl.selectNodes("//Host").get(0);
            MyHost myHost = new MyHost(hostEl.attributeValue("name"), myEngine);

            File contextParentFile = new File(hostEl.attributeValue("appBase"));
            // 这边的解析就按照标准结构吧
            // contextParent - demo1(contextId) - server - servlet (目录+servletName)
            // contextParent - demo2(contextId) - server - servlet (目录+servletName)
            // 这边就是模拟 jar 包的解析 没有去解析对应的 web.xml
            for (int i = 0; i < contextParentFile.listFiles().length; i++) {
                File file = contextParentFile.listFiles()[i];
                URL[] urls = {file.toURI().toURL()};
                URLClassLoader urlClassLoader = new URLClassLoader(urls);
                MyContext myContext = new MyContext(myHost, file.getName());
                String subFile1 = "server.servlet.nxjServlet1";
                String subFile2 = "server.servlet.nxjServlet2";
                if (i == 0) {
                    MyWrapper myWrapper1 = new MyWrapper(myContext, subFile1.split("[.]")[2]);
                    Class class1 = urlClassLoader.loadClass(subFile1);
                    myWrapper1.bindServlet((HttpServlet) class1.newInstance());

                } else {
                    MyWrapper myWrapper2 = new MyWrapper(myContext, subFile2.split("[.]")[2]);
                    Class class2 = urlClassLoader.loadClass(subFile2);
                    myWrapper2.bindServlet((HttpServlet) class2.newInstance());
                }
            }
            // 总体思路就是讲 webapps 下的项目加载到内存map中 通过 host 和 context 的mapper
        } catch (DocumentException e1) {
            e1.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public void startup() throws IOException {
        myServer.start();
    }


}
